import { defineStore } from 'pinia'
import { AuthStateType } from '../types/users'
import api from '../api'
import { RouteRecordRaw } from 'vue-router'
import { getViews } from '../utils/routedync'
const useAuthStore = defineStore("auth", {
    state: (): AuthStateType => {
        return {
            permissionList: []
        }
    },
    getters: {
        getDynicRoute(state: AuthStateType) {
            let homeRouteObj: RouteRecordRaw = {
                path: '/home',
                component: () => import('../views/Home.vue'),
                children: []
            }
            let ary: Array<RouteRecordRaw> = []
            if (state&&state.permissionList) {
                state.permissionList.forEach(item => {
                    if (item.children) {
                        item.children.forEach(subItem => {
                            let routeItem = {
                                path: subItem.path,
                                component: getViews(`../views${subItem.path}.vue`)
                            }
                            ary.push(routeItem)
                        })
                    }
                })
            }
            homeRouteObj.children = ary
            return homeRouteObj
        }
    },
    actions: {
        async getAuthMenuAsync() {
            const result = await api.users.getAuthMenus()
            this.permissionList = result.data.data
        }
    },
    persist: {
        enabled: true,
        strategies: [
            {
                key: 'my_auth',
                storage: localStorage
            }
        ]
    }
})

export default useAuthStore